var dataSource = new Array();
//table的数据源
var table;   //table对象
function stockList(data) {
    console.log(data)
    dataSource.splice(0, dataSource.length);//清空数组
    $.each(data.data, function (i, field) {
        var rowData = new Array();
        console.log(rowData)
        rowData.push(field.address_Name);
        rowData.push(field.warehouse_Name);
        rowData.push(field.stockInto.actual_stock);
        rowData.push(field.stockInto.occupancy_stock);
        rowData.push(field.stockInto.virtual_stock);
        rowData.push(field.stockInto.shop_sku_stock_id);
        dataSource.push(rowData);
    });
    initDataTable(dataSource);
    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "autoWidth": false,
            "ordering": true,
            "searching": true,
            "paging": true,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [5, 10, 25, 50],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function(data,type,row,meta){
                        if(data == null){
                            var val ="";
                            return val;
                        }else {
                            var val = row[0];
                            return val;
                        }
                    },
                    "targets": 0
                },
                {
                    "render": function(data,type,row,meta){
                        if(data == null){
                            var val ="";
                            return val;
                        }else {
                            var val = row[1];
                            return val;
                        }
                    },
                    "targets": 1
                },
                {
                    "render": function(data,type,row,meta){
                        if(data == null){
                            var val = "";
                            return val;
                        }else {
                            var val = row[2];
                            return val;
                        }

                    },
                    "targets": 2
                },
                {
                    "render": function(data,type,row,meta){
                        if(data == null){
                            var val = '--';
                            return val;
                        }else {
                            var val = row[3];
                            return val;
                        }

                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row, meta) {
                        console.log(row)
                        if(data == null){
                            var val = '<span id="' + row[5] + '">' + 0 + '</span>&nbsp;' +
                                '<input value="' +row[4]  + '" class="hide" id="changeNum">' +
                                '<span id="editorIcon" class="Hui-iconfont f-ricon-edit" onclick="edit_stock(' + row[5] + ',' + 0 + ')">&#xe6df; </span>  <button id="change" type="button" onclick="isChange(' + row[5] + ')" class="hide" >确定</button>      <button id="cancel"  type="button"  class="hide" onclick="isCancel(' + row[5] + ',' + row[4] + ')">取消</button>';
                            return val;
                        }else {
                            var val = '<span id="' + row[5] + '">' + row[4] + '</span>&nbsp;' +
                                '<input value="' + row[4] + '" class="hide" id="changeNum">' +
                                '<span id="editorIcon" class="Hui-iconfont f-ricon-edit" onclick="edit_stock(' + row[5] + ',' + row[4] + ')">&#xe6df; </span>  <button id="change" type="button" onclick="isChange(' + row[5] + ')" class="hide" >确定</button>      <button id="cancel"  type="button"  class="hide" onclick="isCancel(' + row[5] + ',' + row[4] + ')">取消</button>';
                            return val;
                        }
                    },
                    "targets": 4
                },
            ]
        });
    };
}

/*表单验证特殊字符*/
function ValidateValue(textbox) {
    var IllegalString = "[`~!#$^&*()=|{}':;',\\[\\].<>/?~！#￥……&*（）——|{}【】‘；：”“'。，、？]‘’";
    var textboxvalue = textbox.value;
    var index = textboxvalue.length - 1;
    var s = textbox.value.charAt(index);
    if (IllegalString.indexOf(s) >= 0) {
        s = textboxvalue.substring(0, index);
        textbox.value = s;
        layer.msg("不能输入特殊字符");
    }
}

/*tab切换*/
jQuery.Huitab = function (tabBar, tabCon, class_name, tabEvent, i) {
    var $tab_menu = $(tabBar);
// 初始化操作
    $tab_menu.removeClass(class_name);
    $(tabBar).eq(i).addClass(class_name);
    $(tabCon).hide();
    $(tabCon).eq(i).show();
    $tab_menu.bind(tabEvent, function () {
        $tab_menu.removeClass(class_name);
        $(this).addClass(class_name);
        var index = $tab_menu.index(this);
        $(tabCon).hide();
        $(tabCon).eq(index).show()
    })
}

//虚拟库存修改
function edit_stock(id) {
    console.log(0)
   $('#'+id+'').addClass('hide');
   $('#changeNum').removeClass('hide');
   $('#change').removeClass('hide');
   $('#cancel').removeClass('hide');
    $('#editorIcon').addClass('hide');
}
function isChange(id) {
    console.log(1)
    $('#'+id+'').removeClass('hide');
    $('#changeNum').addClass('hide');
    $('#change').addClass('hide');
    $('#cancel').addClass('hide');
    $('#editorIcon').removeClass('hide');
    var VirtualStock=$('#changeNum').val();
    var localStorageParameters = decodeURIComponent(web.readCookie("loginData"));
    var JsonObject =JSON.parse(localStorageParameters);
    // var user = loginData.user.name;
    // console.log(user);
    console.log(VirtualStock);
    var stock_data = {
         VirtualStock: VirtualStock,
         ShopSkuStockId: id,
         lastModifiedBy:JsonObject.user.user_ID
     }
     var succCallBack = function (data, status, response) {
             console.log(data);
             if(data.errorCode==0){
                 $('#'+id+'').html(VirtualStock);
             }
     };
     return web.doAjax(root.goodsInter.goods_updateVirtualStockValue, 'post', stock_data, succCallBack);

}
function isCancel(id,oldVal) {
    console.log(2)
    $('#'+id+'').removeClass('hide');
    $('#changeNum').addClass('hide');
    $('#change').addClass('hide');
    $('#cancel').addClass('hide');
    $('#changeNum').val(oldVal);
    $('#'+id+'').html(oldVal);
    $('#editorIcon').removeClass('hide');
}